#include "stdint.h"
#include "fcntl.h"
#include "config.h"
#include "fsk.h"
#include <sys/ioctl.h>
#include <sys/types.h>
#include "running.h"


//#define DEBUG

static int adc_fd = -1;

void adc_init(void)
{
	 adc_fd = open("/dev/adc", O_RDONLY|O_NONBLOCK);
}
static void change_adc_channel(uint8_t g_channel)
{
	ioctl(adc_fd, IOCTL_ADC_CONFIG_CHANNEL, g_channel);
}

unsigned int adc_read(unsigned char ucChannel)
{
    int count;
    uint16_t AdcData[3];

    change_adc_channel(ucChannel);
	read(adc_fd, AdcData, 3);
#ifdef DEBUG
    for(count = 1; count < 3; count++) 
        printf("AdcData[%d]= %x \n", count, AdcData[count]);
#endif
    return ((AdcData[1] + AdcData[2]) / 2);
}

